home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Tools / Alpha 6.51b13 ƒ / Tcl / SystemCode / library.tcl < prev    next >
Text File  |  1997-03-26  |  5KB  |  189 lines

  1. # init.tcl --
  2. #
  3. # Default system startup file for Tcl-based applications.  Defines
  4. # "unknown" procedure and auto-load facilities.
  5. #
  6. # $Header: /rel/cvsfiles/devo/tcl/library/init.tcl,v 1.2 1992/12/23 15:39:29 zoo Exp $ SPRITE (Berkeley)
  7. #
  8. # Copyright 1991-1992 Regents of the University of California
  9. # Permission to use, copy, modify, and distribute this
  10. # software and its documentation for any purpose and without
  11. # fee is hereby granted, provided that this copyright
  12. # notice appears in all copies.  The University of California
  13. # makes no representations about the suitability of this
  14. # software for any purpose.  It is provided "as is" without
  15. # express or implied warranty.
  16. #
  17.  
  18. # unknown:
  19. # Invoked when a Tcl command is invoked that doesn't exist in the
  20. # interpreter:
  21. #
  22. #    1. See if the autoload facility can locate the command in a
  23. #       Tcl script file.  If so, load it and execute it.
  24. #    2. See if the command exists as an executable UNIX program.
  25. #       If so, "exec" the command.
  26. #    3. If the command was invoked at top-level:
  27. #        (a) see if the command requests csh-like history substitution
  28. #        in one of the common forms !!, !<number>, or ^old^new.  If
  29. #        so, emulate csh's history substitution.
  30. #        (b) see if the command is a unique abbreviation for another
  31. #        command.  If so, invoke the command.
  32.  
  33. proc unknown args {
  34.     global auto_noload env unknown_pending;
  35.  
  36.     set name [lindex $args 0]
  37.  
  38.     if {[string length [set f [findCmd $name]]]} {
  39.         uplevel #0 source [list $f]
  40.         return [uplevel $args]
  41.     } else {
  42.         error "No such function: $name"
  43.     }
  44. }
  45.  
  46. # auto_mkindex:
  47. # Regenerate a tclIndex file from Tcl source files.  Takes two arguments:
  48. # the name of the directory in which the tclIndex file is to be placed,
  49. # and a glob pattern to use in that directory to locate all of the relevant
  50. # files.
  51. proc auto_mkindex {dir files} {
  52.     global alphaLite
  53.     
  54.     set oldDir [pwd]
  55.     cd $dir
  56.     append line "# Tcl autoload index file: each line identifies a file (nowrap)\n\n"
  57.     append line "set [file tail [string trim [pwd] :]]_index \{\n"
  58.  
  59.     set cid [scancontext create]
  60.     scanmatch $cid {^proc[     ]} {
  61.         if {[regexp {^proc[     ]+([^     ]*)} $matchInfo(line) match procName]} {
  62.             append line "$procName "
  63.         }
  64.     }
  65.  
  66.     foreach file [glob $files] {
  67.         if {($file == "menusLite.tcl") && !$alphaLite} continue;
  68.         watchCursor
  69.         set f ""
  70.         append line "\{[file tail $file] "
  71.         message [file tail $file]
  72.         set fid [open $file]
  73.         scanfile $cid $fid
  74.         close $fid
  75.         append line "\}\n"
  76.     }
  77.     
  78.     scancontext delete $cid
  79.  
  80.     append line "\}\n"
  81.     set f [open tclIndexx w]
  82.     puts $f $line nonewline
  83.     close $f
  84.     cd $oldDir
  85.  
  86.     foreach i [info vars {*_index}] {
  87.         global $i
  88.         unset $i
  89.     }
  90. }
  91.  
  92. proc findCmd cmd {
  93.     global global auto_path
  94.     
  95.     foreach path $auto_path {
  96.         if {![file exists $path]} continue
  97.         set index "[file tail $path]_index"
  98.         global $index
  99.         if {![info exists $index]} {
  100.             uplevel #0 source [list "$path:tclIndexx"]
  101.         }
  102.         if {[regexp "\{(\\w+.tcl)\[^\}\]* [quoteExpr $cmd] " [set $index] dummy file]} {
  103.             return "$path:$file"
  104.         }
  105.     }
  106. }
  107.  
  108.  
  109. #================================================================================
  110. # Wonderful procs from Vince Darley (vince@das.harvard.edu).
  111. #===============================================================================
  112.  
  113. proc traceTclProc {} {
  114.     global tclMenu
  115.     if {[llength [traceFunc status]]>2} {
  116.         catch {markMenuItem $tclMenu {traceTclProc…} off}
  117.         catch {enableMenuItem $tclMenu dumpTraces off}
  118.         if {[string length [set data [traceDump]]]} {
  119.             if {[askyesno "Dump traces?"] == "yes"} {
  120.                 dumpTraces [string trimright [lindex [traceFunc status] 3] {,}] $data
  121.                 setWinInfo dirty 0
  122.             }
  123.         }
  124.         traceFunc off
  125.         message "Tracing off."
  126.         return
  127.     }
  128.     if {[llength [winNames]] && [string length [set sel [getSelect]]]} {
  129.         set func [listpick -L $sel -p {Func Name:} [lsort -ignore [info procs]]]
  130.     } else {
  131.         set func [listpick -p {Func Name:} [lsort -ignore [info procs]]]
  132.     }
  133.     if {![string length $func]} return
  134.     traceFunc on $func ""
  135.     catch {markMenuItem $tclMenu {traceTclProc…} on}
  136.     catch {enableMenuItem $tclMenu dumpTraces on}
  137.     message "Tracing '$func'…"
  138. }
  139.  
  140.  
  141. proc dumpTraces {{name ""} {data ""}} {
  142.     if {![string length $name]} {
  143.         set name [string trimright [lindex [traceFunc status] 3] {,}]
  144.     }
  145.     if {![string length $data]} {
  146.         set data [traceDump]
  147.     }
  148.     
  149.     if {![string length $data]} {
  150.         message "Trace buffer empty"
  151.     } else {
  152.         regsub -all {:} $name {.} name
  153.         new -n "* Trace '$name' *"
  154.         insertText $data
  155.         setWinInfo dirty 0
  156.         goto 0 
  157.     }
  158. }
  159.  
  160.  
  161. proc rebuildTclIndices {} {
  162.     global auto_path
  163.     set d [pwd]
  164.     # do we really need the next line? Alpha's original uses it.
  165.     cd
  166.     foreach dir $auto_path {
  167.         # if directory exists
  168.         if { ![catch { cd $dir } ] } {
  169.             # if there are any files
  170.             if { ![catch { glob *.*tcl } ] } {
  171.                 message "Building [file tail $dir] index…"
  172.                 
  173.                 # if the '[incr tcl]' version exists, use that
  174.                 # use 'catch' also in case directory is write-protected
  175.                 if [catch { itcl_mkindex : *.*tcl } ] {
  176.                     # else try the normal one
  177.                     catch { auto_mkindex : *.*tcl }
  178.                 }
  179.             }
  180.         }
  181.     }
  182.     # redo the auto-mode-file connections (see "smarterSource.tcl")
  183.     message "Building the mode-file dependency array"
  184.     catch {autoModeFiles}
  185.     message ""
  186.     cd $d
  187. }
  188.  
  189.